﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ncp.Business;
using ncp.Business.Common;
using ncp.Business;

namespace ncp.Web.cms.work.CommentManager
{
    public partial class Comment_Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
            if (userInfo == null)
            {
                Response.Write(" <script> top.location.href='/cms/login.aspx'; </script> ");
            }
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Comment_Index));
        }

        //得到总记录数
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public int GetRecordCount(string currentwhere)
        {
            return CommentInfoBll.GetCommentInfoCount(currentwhere, CommonConst.DB_Name);
        }

        //得到初始条件
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetstrWhere(string pQueryName, string pQueryOperator, string pQueryValue)
        {
            string myWhere = "1=1";
            if (pQueryValue != "")
            {
                if (pQueryOperator == "like")
                {
                    myWhere += " and " + pQueryName + " like '%" + pQueryValue + "%'";
                }
                else
                {
                    myWhere += " and " + pQueryName + " " + pQueryOperator + " '" + pQueryValue + "'";
                }
            }
            return myWhere;
        }

        //装载评论表视图
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string BindTable(string jsonData)
        {
            var DictList = JsonHelper.JSONToObject<Dictionary<string, object>>(jsonData);
            DataTable dt = CommentInfoBll.GetCommentInfoTable(int.Parse(DictList["currentpageindex"].ToString()) - 1, int.Parse(DictList["pagesize"].ToString()), "*", DictList["sorting"] + " " + DictList["sortdir"], DictList["currentwhere"].ToString(), CommonConst.DB_Name);
            return JsonHelper.DataTableToJSON(dt);
        }

        //设置评论
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string UpdateCommentStatus(int Comment_Id, int Comment_Status)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                int ii = CommentInfoBll.UpdateCommentStatus(Comment_Id.ToString(), Comment_Status.ToString(), CommonConst.DB_Name);
                if (ii == 0)
                {
                    jsonResult.Add("msg", "设置不成功！");
                    jsonResult.Add("isreload", false);
                }
                else
                {
                    var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                    Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                    dicHandleLog.Add("user_id", userInfo.Id);
                    dicHandleLog.Add("computername", userInfo.Computername);
                    dicHandleLog.Add("computerip", userInfo.Computerip);
                    dicHandleLog.Add("handle_note", (Comment_Status == 0 ? "禁用评论" : "启动评论"));
                    dicHandleLog.Add("obj_guid", "Comment_Id=" + Comment_Id.ToString());
                    LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);
                    jsonResult.Add("msg", "设置成功！");
                    jsonResult.Add("isreload", true);
                }
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //删除评论信息
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string DeleteNewsInfo(int commentId)
        {
            var jsonResult = new Dictionary<string, object>();
            try
            {
                int ii = CommentInfoBll.DeleteCommentInfo(commentId.ToString(), CommonConst.DB_Name);

                var userInfo = WebMasterCookie.GetCookie(CommonConst.adminCookieName);
                Dictionary<string, object> dicHandleLog = new Dictionary<string, object>();
                dicHandleLog.Add("user_id", userInfo.Id);
                dicHandleLog.Add("computername", userInfo.Computername);
                dicHandleLog.Add("computerip", userInfo.Computerip);
                dicHandleLog.Add("handle_note", "删除评论");
                dicHandleLog.Add("obj_guid", "commentId=" + commentId.ToString());
                LogHandleBll.AddHandleLog(dicHandleLog, CommonConst.DB_Name);
                jsonResult.Add("msg", "删除成功！");
                jsonResult.Add("isreload", true);
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string GetContent(int commentId)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                var dt = CommentInfoBll.GetCommentById(commentId.ToString(), CommonConst.DB_Name);
                string content = "";
                if (dt.Rows.Count > 0)
                {
                    content = dt.Rows[0]["Comment_Note"].ToString();
                }
                jsonResult.Add("isreload", true);
                jsonResult.Add("msg", content);
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("msg", ex.Message);
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
